package com.sqllog.admin.sql.controller;


import com.sqllog.admin.auth.domain.response.Result;
import com.sqllog.admin.auth.domain.response.module.ResultTable;
import com.sqllog.admin.framework.controller.ResultController;
import com.sqllog.admin.framework.tools.SequenceUtil;
import com.sqllog.admin.sql.entity.CanalClientConfigInfo;
import com.sqllog.admin.sql.entity.CanalClientConsumerLog;
import com.sqllog.admin.sql.service.CanalClientConfigInfoService;
import com.sqllog.admin.sql.service.CanalClientConsumerLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


/**
 * Sequence 工 具 类
 *
 * @author chennengcheng
 * @date 2022-01-07 17:19
 * @remark
 */

@RestController
@RequestMapping("/canalClient/")
public class CanalClientConfigController extends ResultController {

    /**
     * Describe: 基础路径
     */
    private static String MODULE_PATH = "canalClient/";

    @Autowired
    private CanalClientConfigInfoService canalClientConfigInfoService;


    @Autowired
    private CanalClientConsumerLogService canalClientConsumerLogService;

    @GetMapping("main")
    public ModelAndView main(ModelAndView modelAndView) {
        return jumpPage(MODULE_PATH + "main");
    }

    @GetMapping("data")
    public ResultTable data(String name) {
        return dataTable(canalClientConfigInfoService.listCanalClientConfigByName(name));
    }

    @GetMapping("add")
    public ModelAndView add() {
        return jumpPage(MODULE_PATH + "add");
    }

    @GetMapping("edit")
    public ModelAndView edit(Model model, @RequestParam("id") String id) {
        model.addAttribute("canalConfig", canalClientConfigInfoService.selectById(id));
        return jumpPage(MODULE_PATH + "edit");
    }

    @PostMapping("save")
    public Result save(@RequestBody CanalClientConfigInfo canalClientConfigInfo) {
        canalClientConfigInfo.setId(SequenceUtil.makeStringId());
        boolean result = canalClientConfigInfoService.addListenerInstance(canalClientConfigInfo);
        return decide(result);
    }

    @PutMapping("update")
    public Result update(@RequestBody CanalClientConfigInfo canalClientConfigInfo) {
        boolean result = canalClientConfigInfoService.updateListenerInstance(canalClientConfigInfo);
        return decide(result);
    }

    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable String id) {
        boolean result = canalClientConfigInfoService.deleteById(id);
        return decide(result);
    }

    @PutMapping("startClient/{id}")
    public Result<Boolean> startClient(@PathVariable String id) {
        boolean result = canalClientConfigInfoService.startClientListenerInstance(id);
        return decide(result);
    }

    @PutMapping("stopClient/{id}")
    public Result<Boolean> stopClient(@PathVariable String id) {
        boolean result = canalClientConfigInfoService.stopClientListenerInstance(id);
        return decide(result);
    }


    @PutMapping("disable")
    public Result<Boolean> disable(@RequestBody CanalClientConfigInfo canalClientConfigInfo) {
        canalClientConfigInfo.setStatus("0");
        boolean result = canalClientConfigInfoService.updateListenerInstance(canalClientConfigInfo);
        return decide(result);
    }


    @GetMapping("detail/{id}")
    public ResultTable detail(@PathVariable String id) {
        List<CanalClientConsumerLog> canalClientConsumerLogs = canalClientConsumerLogService.selectByClientId(id);
        return dataTable(canalClientConsumerLogs);
    }
}
